package org.jeecg.modules.quartz.job;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.system.controller.SysUserController;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.finer.erp.jeecg.bas.entity.BasCustomer;
import io.finer.erp.jeecg.bas.entity.ZrkCollectionRate;
import io.finer.erp.jeecg.bas.entity.ZrkOrderSalary;
import io.finer.erp.jeecg.bas.entity.ZrkShoporder;
import io.finer.erp.jeecg.bas.entity.ZrkUserCustomer;
import io.finer.erp.jeecg.bas.entity.ZrkYwSalary;
import io.finer.erp.jeecg.bas.service.IBasCustomerService;
import io.finer.erp.jeecg.bas.service.IZrkCollectionRateService;
import io.finer.erp.jeecg.bas.service.IZrkOrderSalaryService;
import io.finer.erp.jeecg.bas.service.IZrkSalaryCalService;
import io.finer.erp.jeecg.bas.service.IZrkShoporderService;
import io.finer.erp.jeecg.bas.service.IZrkUserCustomerService;
import io.finer.erp.jeecg.bas.service.IZrkYwSalaryService;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class ComeOrderGenerateSalaryOfYW implements Job {
	
	@Autowired
	private IZrkShoporderService zrkShoporderService;
	
	@Autowired
	private IZrkYwSalaryService zrkYsSalaryService;
	
	@Autowired
	private IZrkSalaryCalService zrkSalaryCalService;

	@Autowired
	private IZrkOrderSalaryService zrkOrderSalaryService;

	@Autowired
	private IZrkCollectionRateService zrkCollectionRateService;
	
	@Autowired
	private IBasCustomerService basCustomerService;
	
	@Autowired
	private IZrkUserCustomerService userCustomerService;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		
		//查询未生成业务薪资数据的销售订单
	    QueryWrapper<ZrkShoporder> queryWrapper=new QueryWrapper<ZrkShoporder>();
	    queryWrapper.eq("is_salary", "0");
	    List<ZrkShoporder> zsoList=zrkShoporderService.list(queryWrapper);
	    if(null!=zsoList&&zsoList.size()>0) {
	    	for(ZrkShoporder zos:zsoList) {
	    		//增加判断销售订单是否为可以生成薪资阶段
	    		if(zos.getOrderStatus().equals("12")) {
	    			ZrkOrderSalary zrkOrderSalary=new ZrkOrderSalary();
		    		zrkOrderSalary.setUserId(zos.getSalsUser());//设置业务员ID
		    		zrkOrderSalary.setOrderId(zos.getShopOrder());//设置商城订单号
		    		zrkOrderSalary.setSalesMoney(zos.getOrderMoney());//设置订单金额
		    		zrkOrderSalary.setIsAll(0);//是否全部回款
		    		zrkOrderSalary.setOrderTime(zos.getOrderTime());//订单创建时间
		    		zrkOrderSalary.setSalaryStatus(1);//设置状态为未计算
		    		//查询客户信息
		    		BasCustomer bc=basCustomerService.getById(zos.getCustomerId());
	    			zrkOrderSalary.setMoneyType(Integer.parseInt(bc.getCashType()));//设置回款类型
	    			zrkOrderSalary.setCusId(bc.getId());//设置客户编号
	    			//判断业务员是否为空
	    			if(StringUtils.isBlank(zos.getSalsUser())) {
	    				ZrkUserCustomer userCustomer = userCustomerService.getByCustomerId(bc.getId());
	    				if(null == userCustomer) {
	    					continue;
	    				} else {
	    					String salesUserId = userCustomer.getUserId();
	    					zos.setSalsUser(salesUserId);
	    					zrkOrderSalary.setUserId(zos.getSalsUser());//设置业务员ID
	    				}
	    			}
		    		//判断新老客户
		    		ZrkShoporder zs=zrkShoporderService.selectNewOrOldCustomer(zos.getCustomerId());
		    		//查询业务员参数
		    		QueryWrapper<ZrkYwSalary> zscWrapper=new QueryWrapper<ZrkYwSalary>();
		    		zscWrapper.eq("user_id", zos.getSalsUser());
		    		ZrkYwSalary zrkYwSalary=zrkYsSalaryService.getOne(zscWrapper.last("LIMIT 1"));
		    		if(null == zrkYwSalary) {
		    			continue;
		    		}
		    		if(zs.getIsPush().equals("1")) {
		    			//老客户
		    			BigDecimal thirtyMoney=zos.getOrderMoney().multiply(zrkYwSalary.getOldCusRate()).multiply(new BigDecimal("0.3"));
		    			BigDecimal seventyMoney=zos.getOrderMoney().multiply(zrkYwSalary.getOldCusRate()).multiply(new BigDecimal("0.7"));
		    			zrkOrderSalary.setThirtyMoney(thirtyMoney);
		    			zrkOrderSalary.setSenventyMoney(seventyMoney);
		    			zrkOrderSalary.setIsNewOrOld(0);
		    		}else {
		    			//新客户
		    			BigDecimal thirtyMoney=zos.getOrderMoney().multiply(zrkYwSalary.getNewCusRate()).multiply(new BigDecimal("0.3"));
		    			BigDecimal seventyMoney=zos.getOrderMoney().multiply(zrkYwSalary.getNewCusRate()).multiply(new BigDecimal("0.7"));
		    			zrkOrderSalary.setThirtyMoney(thirtyMoney);
		    			zrkOrderSalary.setSenventyMoney(seventyMoney);
		    			zrkOrderSalary.setIsNewOrOld(1);
		    		}
		    		//计算该工资表的有效时间
		    		String endTime=DateUtils.addDayNums(bc.getZhangqi(), zos.getOrderTime());
		    		QueryWrapper<ZrkCollectionRate> zrcWrapper=new QueryWrapper<ZrkCollectionRate>();
		    		ZrkCollectionRate zcr=zrkCollectionRateService.getOne(zrcWrapper.last("LIMIT 1"));
		    		String maxTime=DateUtils.addDayNums(zcr.getLevelThree(), zos.getOrderTime());
		    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		    		zrkOrderSalary.setStartTime(DateUtils.str2Date(sdf.format(zos.getOrderTime()), sdf));//设置开始日期，订单成交时间
		    		zrkOrderSalary.setEndTime(DateUtils.str2Date(endTime, sdf));//设置结束日期
		    		zrkOrderSalary.setOvertime(DateUtils.str2Date(maxTime, sdf));//设置最高有效期
		    		//处理完成后提交
		    		zrkOrderSalaryService.save(zrkOrderSalary);
		    		zos.setIsSalary("1");
		    		zrkShoporderService.updateById(zos);//更新销售订单中是否计算工资标志改为1
	    		}else {
	    			log.info("销售订单状态不是可以生成业务薪资的状态，状态码=="+zos.getOrderStatus());
	    		}
	    	}
	    }
        
	}

}
